home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <malloc.h>
- #include "TimeGrid.h"
- #include "Utils.h"
- #include "TimeEntry.h"
- #include "Preferences.h"
- #include <Vk/VkResource.h>
-
- #include <Xm/LabelG.h>
- #include <Xm/Label.h>
- #include <Xm/SeparatoG.h>
- #include <Xm/Separator.h>
- #include <Xm/Form.h>
- #include <Xm/RowColumn.h>
- #include <Xm/DrawingA.h>
- #include <Xm/BulletinB.h>
-
- static int count;
- static Arg args[10];
-
- TimeGrid::TimeGrid(const char *name, Widget parent, Boolean editable,
- int timeInterval, int subdivisions,
- int start, int length)
- : VkComponent(name)
- {
- _timeInt = timeInterval;
- _subdiv = subdivisions;
- _start = start;
- _length = length;
-
- _marginWidth = 10;
- _spacing = 10;
- _width = _height = 0;
- _callback = NULL;
-
- _start = _length = 0;
- _numTime = 0;
- _areas = NULL;
- _textFontList = NULL;
- _currentTime = NULL;
- _text = NULL;
-
- count = 0;
- XtSetArg(args[count], XmNmarginWidth, 0); count++;
- XtSetArg(args[count], XmNmarginHeight, 0); count++;
- XtSetArg(args[count], XmNallowOverlap, True); count++;
- XtSetArg(args[count], XmNshadowThickness, 0); count++;
- _baseWidget = XmCreateBulletinBoard(parent, "timeGrid", args, count);
- XtAddEventHandler(_baseWidget, StructureNotifyMask, False,
- TimeGrid::reconfig_stub, (XtPointer) this);
- XtAddEventHandler(_baseWidget, ButtonPressMask, False,
- TimeGrid::button_press, (XtPointer) this);
- installDestroyHandler();
-
- _slotHeight = (int) VkGetResource(_baseWidget, "slotHeight", "SlotHeight",
- XmRInt, "50");
-
- changeDuration(start, length);
-
- _text = new TimeEntryText("timeGridText", this, editable);
- count = 0;
- XtSetArg(args[count], XmNfontList, &_textFontList); count++;
- XtGetValues(_text->baseWidget(), args, count);
-
- count = 0;
- XtSetArg(args[count], XmNx, 0); count++;
- _currentTime = XmCreateSeparator(_baseWidget, "currentTime", args, count);
- }
-
- TimeGrid::~TimeGrid()
- {
- delete _text;
- free(_areas);
- if (_baseWidget) {
- XtRemoveEventHandler(_baseWidget, StructureNotifyMask, False,
- TimeGrid::reconfig_stub, (XtPointer) this);
- XtRemoveEventHandler(_baseWidget, ButtonPressMask, False,
- TimeGrid::button_press, (XtPointer) this);
- }
- }
-
- const char *
- TimeGrid::className()
- {
- return "TimeGrid";
- }
-
- /**********************************************************************/
-
- void
- TimeGrid::setCallback(XtCallbackProc proc, XtPointer client_data)
- {
- _callback = proc;
- _callbackData = client_data;
- }
-
- void
- TimeGrid::changeDuration(int start, int length)
- {
- if (_start != start || _length != length) {
- if (_areas) {
- destroyAreas();
- free(_areas);
- }
- if (_currentTime) {
- XtUnmanageChild(_currentTime);
- }
- _start = start;
- _length = length;
- _numTime = (_length)/_timeInt;
- _areas = (AreaInfo *) malloc(_numTime*sizeof(AreaInfo));
- createAreas(_baseWidget);
- count = 0;
- XtSetArg(args[count], XmNheight, getHeight()); count++;
- XtSetValues(_baseWidget, args, count);
- XtUnmanageChild(_baseWidget);
- XtManageChild(_baseWidget);
- reconfig(True);
- }
- }
-
- Boolean
- TimeGrid::requestPosition(int start, int length,
- Position *x_return, Position *y_return,
- Dimension *width_return, Dimension *height_return)
- {
- if (start < _start /* || start+length > _start+_length */ || !_width) {
- return False;
- } else {
- *x_return = _marginWidth+_maxWidth+_spacing+_leftW;
- *y_return = ((start-_start)*_slotHeight*_subdiv)/_timeInt+_sepH;
- *width_return = _width-2*_marginWidth-_maxWidth-_leftW-_rightW-_spacing;
- *height_return = (length*_slotHeight*_subdiv)/_timeInt-_sepH;
-
- return True;
- }
- }
-
- Boolean
- TimeGrid::requestWeekdayPosition(int start, int length, int weekday,
- Position *x_return, Position *y_return,
- Dimension *width_return,
- Dimension *height_return)
- {
- Dimension offset;
-
- if (requestPosition(start, length, x_return, y_return,
- width_return, height_return)) {
- offset = (*width_return)/DAYS_IN_WEEK;
- *x_return = (*x_return)+(weekday-1)*offset;
- *width_return = offset;
- return True;
- } else {
- return False;
- }
- }
-
- Boolean
- TimeGrid::requestIntervalPosition(int start, int length,
- Position *x_return, Position *y_return,
- Dimension *width_return,
- Dimension *height_return)
- {
- if (start < _start /* || start+length > _start+_length */ || !_width) {
- return False;
- } else {
- *x_return = _marginWidth;
- *y_return = ((start-_start)*_slotHeight*_subdiv)/_timeInt+_sepH;
- *width_return = _maxWidth+_spacing;
- *height_return = (length*_slotHeight*_subdiv)/_timeInt-_sepH;
-
- return True;
- }
- }
-
- void
- TimeGrid::requestInterval(Position y, Dimension height,
- int *start_return, int *length_return)
- {
- *start_return = ((y-_sepH)*_timeInt)/_slotHeight/_subdiv;
- *start_return = *start_return-(*start_return % (_timeInt/_subdiv))+_start;
- *length_return = ((height+_sepH)*_timeInt)/_slotHeight/_subdiv;
- }
-
- void
- TimeGrid::reconfig(Boolean always)
- {
- Dimension width, height;
- int each;
-
- count = 0;
- XtSetArg(args[count], XmNwidth, &width); count++;
- XtSetArg(args[count], XmNheight, &height); count++;
- XtGetValues(_baseWidget, args, count);
- if (always || width != _width || height != _height) {
- _width = width;
- _height = height;
- count = 0;
- XtSetArg(args[count], XmNwidth, &_leftW); count++;
- XtGetValues(_left, args, count);
- count = 0;
- XtSetArg(args[count], XmNwidth, &_rightW); count++;
- XtGetValues(_right, args, count);
- count = 0;
- XtSetArg(args[count], XmNheight, &_sepH); count++;
- XtGetValues(_areas[0].separator, args, count);
-
- count = 0;
- XtSetArg(args[count], XmNwidth, _width-2*_marginWidth); count++;
- XtSetValues(_bottom, args, count);
- for (each=0; each<_numTime; each++) {
- XtSetValues(_areas[each].separator, args, count);
- }
- count = 0;
- XtSetArg(args[count], XmNheight, getHeight()); count++;
- XtSetValues(_left, args, count);
- count = 0;
- XtSetArg(args[count], XmNx, _width-_marginWidth-_rightW); count++;
- XtSetArg(args[count], XmNheight, getHeight()); count++;
- XtSetValues(_right, args, count);
- if (_text) {
- _text->reposition();
- }
- if (_currentTime) {
- count = 0;
- XtSetArg(args[count], XmNwidth, width); count++;
- XtSetValues(_currentTime, args, count);
- }
- }
- }
-
- void
- TimeGrid::reset()
- {
- _text->hide();
- _text->setCallback(NULL, NULL);
- }
-
- void
- TimeGrid::hideCurrentTime()
- {
- if (_currentTime) {
- XtUnmanageChild(_currentTime);
- }
- }
-
- void
- TimeGrid::showCurrentTime(int start)
- {
- Position y, ignore_p;
- Dimension ignore_d;
-
- if (_currentTime) {
- if (requestPosition(start, 0, &ignore_p, &y, &ignore_d, &ignore_d)) {
- count = 0;
- XtSetArg(args[count], XmNy, y); count++;
- XtSetValues(_currentTime, args, count);
- XtManageChild(_currentTime);
- if (XtWindow(_currentTime)) {
- XRaiseWindow(XtDisplay(_currentTime), XtWindow(_currentTime));
- }
- } else {
- hideCurrentTime();
- }
- }
- }
-
- /**********************************************************************/
-
- void
- TimeGrid::createAreas(Widget parent)
- {
- int each;
- char str[256];
- XmString xs;
- Dimension width;
-
- // Vertical separators
- count = 0;
- XtSetArg(args[count], XmNx, 0); count++;
- XtSetArg(args[count], XmNy, 0); count++;
- XtSetArg(args[count], XmNorientation, XmVERTICAL); count++;
- _right = XmCreateSeparatorGadget(parent, "rightSeparator", args, count);
- XtManageChild(_right);
- count = 0;
- XtSetArg(args[count], XmNx, 0); count++;
- XtSetArg(args[count], XmNy, 0); count++;
- XtSetArg(args[count], XmNorientation, XmVERTICAL); count++;
- _left = XmCreateSeparatorGadget(parent, "leftSeparator", args, count);
- XtManageChild(_left);
-
- // Separators and time labels
- for (each=0; each<_numTime; each++) {
- count = 0;
- XtSetArg(args[count], XmNx, _marginWidth); count++;
- XtSetArg(args[count], XmNy,
- each*_subdiv*_slotHeight); count++;
- XtSetArg(args[count], XmNorientation, XmHORIZONTAL); count++;
- _areas[each].separator = XmCreateSeparatorGadget(parent, "separator",
- args, count);
- count = 0;
- XtSetArg(args[count], XmNheight, &_horizSepHeight); count++;
- XtGetValues(_areas[each].separator, args, count);
-
- formatTime((_start+each*_timeInt) / 60, (_start+each*_timeInt) % 60,
- thePreferences->clock24(), str);
- xs = XmStringCreateSimple(str);
- count = 0;
- XtSetArg(args[count], XmNx, _marginWidth); count++;
- XtSetArg(args[count], XmNy,
- each*_subdiv*_slotHeight+_horizSepHeight); count++;
- XtSetArg(args[count], XmNalignment, XmALIGNMENT_END); count++;
- XtSetArg(args[count], XmNrecomputeSize, False); count++;
- XtSetArg(args[count], XmNlabelString, xs); count++;
- _areas[each].timeLabel = XmCreateLabel(parent, "timeLabel",
- args, count);
- XtAddEventHandler(_areas[each].timeLabel, ButtonPressMask, False,
- TimeGrid::button_press, (XtPointer) this);
- XmStringFree(xs);
-
- XtManageChild(_areas[each].separator);
- XtManageChild(_areas[each].timeLabel);
- }
-
- // Make the time labels right-aligned
- _maxWidth = 0;
- count = 0;
- XtSetArg(args[count], XmNwidth, &width); count++;
- for (each=0; each<_numTime; each++) {
- XtGetValues(_areas[each].timeLabel, args, count);
- if (width > _maxWidth) {
- _maxWidth = width;
- }
- }
- count = 0;
- XtSetArg(args[count], XmNwidth, _maxWidth); count++;
- for (each=0; each<_numTime; each++) {
- XtSetValues(_areas[each].timeLabel, args, count);
- }
-
- // Move the left separator over
- count = 0;
- XtSetArg(args[count], XmNx, _marginWidth+_maxWidth+_spacing); count++;
- XtSetValues(_left, args, count);
-
- // Bottom separator
- count = 0;
- XtSetArg(args[count], XmNx, _marginWidth); count++;
- XtSetArg(args[count], XmNy,
- _numTime*_subdiv*_slotHeight+_horizSepHeight); count++;
- XtSetArg(args[count], XmNorientation, XmHORIZONTAL); count++;
- _bottom = XmCreateSeparatorGadget(parent, "bottomSeparator", args, count);
- XtManageChild(_bottom);
- }
-
- void
- TimeGrid::destroyAreas()
- {
- int each;
-
- XtDestroyWidget(_right);
- XtDestroyWidget(_left);
- XtDestroyWidget(_bottom);
- for (each=0; each<_numTime; each++) {
- XtDestroyWidget(_areas[each].separator);
- XtDestroyWidget(_areas[each].timeLabel);
- }
- }
-
- void
- TimeGrid::callCallback(TimeGridReason reason, int value, void *d)
- {
- TimeGridCallback data;
-
- if (_callback) {
- data.reason = reason;
- data.value = value;
- data.data = d;
- (*_callback)(_baseWidget, _callbackData, (XtPointer) &data);
- }
- }
-
- void
- TimeGrid::buttonPress(XEvent *event)
- {
- int start, ignore;
-
- if (event->xbutton.button == Button1) {
- if (event->xbutton.x < _marginWidth+_maxWidth+_spacing ||
- event->xbutton.x >= _width-_marginWidth) {
- callCallback(TG_deselect);
- } else {
- requestInterval(event->xbutton.y, 0, &start, &ignore);
- callCallback(TG_requestNew, start, event);
- }
- }
- }
-
- /**********************************************************************/
-
- void
- TimeGrid::reconfig_stub(Widget, XtPointer client_data, XEvent *, Boolean *)
- {
- TimeGrid *obj = (TimeGrid *) client_data;
-
- obj->reconfig();
- }
-
- void
- TimeGrid::button_press(Widget, XtPointer client_data, XEvent *event, Boolean *)
- {
- TimeGrid *obj = (TimeGrid *) client_data;
-
- obj->buttonPress(event);
- }
-
-